package com.styytech.dadabao.test;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.lidroid.xutils.view.annotation.ContentView;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.ui.main.FragmentMain;
import com.styytech.dadabao.utils.UpdateAppUtils;

@ContentView(R.layout.activity_main)
public class MainBaseActivity extends BaseActivity {
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    /** 主界面 Fragment */
    private FragmentMain fragmentMain;
//测试就好而得分
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        UpdateAppUtils updateAppUtils=new UpdateAppUtils(this);
//        updateAppUtils.checkingVersion();
    }

    private void initData() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        SDKInitializer.initialize(getApplicationContext());//百度地图必须先初始化

    }

    /**
     * 初始化界面
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setFragment();
    }

    /**
     * 设置Fragment，显示主界面
     */
    private void setFragment() {
        fragmentMain = (FragmentMain) fragmentManager
                .findFragmentByTag("ID_MAIN");
        if (fragmentMain == null) {
            // 如果MessageFragment为空，则创建一个并添加到界面上
            fragmentMain = new FragmentMain();
            transaction.add(R.id.fl_main_content, fragmentMain, "ID_MAIN");
        } else {
            // 如果MessageFragment不为空，则直接将它显示出来
            transaction.show(fragmentMain);
        }
        transaction.commit();
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                DadaApplication.exit();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
